package cn.controlsys.controller;

import cn.controlsys.common.constants.HttpConstant;
import cn.controlsys.common.entity.RequestResult;
import cn.controlsys.model.RechargeAccount;
import cn.controlsys.service.RechargeAccountService;
import cn.controlsys.service.SaiheyiService;
import cn.controlsys.utils.HttpClientResult;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: Created by Administrator on 2018/12/20.
 */
@RestController
@Api(value = "赛合一缴费接口", description = "赛合一缴费接口")
@RequestMapping(value = "v0/saiheyi")
public class SaiheyiController  {
    private static Logger logger = LoggerFactory.getLogger(SaiheyiController.class);
    @Autowired
    SaiheyiService saiheyiService;
    @Autowired
    RechargeAccountService rechargeAccountService;

    @ApiOperation(value = "水电煤账单查询业务订单下单(1)", notes = "水电煤账单查询业务订单下单")
    @RequestMapping(value = "wecbillpost", method = RequestMethod.POST)
    public Object wecbillpost(@ApiParam(value = "产品编号", required = true) @RequestParam String productid,
                              @ApiParam(value = "水电煤缴费账号", required = true) @RequestParam String wecaccount,
                              @ApiParam(value = "",required = true) @RequestParam String ymonth){
        return saiheyiService.wecbillpost(productid, wecaccount, ymonth);
    }

    @ApiOperation(value = "水电煤账单查询(2)", notes = "水电煤账单查询")
    @RequestMapping(value = "wecbillquery", method = RequestMethod.POST)
    public Object wecbillquery(@ApiParam(value = "商家订单号", required = true) @RequestParam String stockordernumber){
        return saiheyiService.wecbillquery(stockordernumber);
    }

    @ApiOperation(value = "水电煤缴费业务订单下单接口(3)", notes = "水电煤缴费业务订单下单接口")
    @RequestMapping(value = "wrecharge", method = RequestMethod.POST)
    public Object wrecharge(@ApiParam(value = "产品编号", required = true) @RequestParam String productid,
                            @ApiParam(value = "滞纳金", required = true) @RequestParam String delayfee,
                            @ApiParam(value = "水电煤缴费账号", required = true) @RequestParam String wecaccount,
                            @ApiParam(value = "水电煤缴费金额", required = true) @RequestParam String wecbillmoney ){
        return saiheyiService.wrecharge(productid, delayfee, wecaccount, wecbillmoney);
    }


    @ApiOperation(value = "根据商家订单号查询订单状态(4)", notes = "根据商家订单号查询订单状态")
    @RequestMapping(value = "orderStatus", method = RequestMethod.POST)
    public Object orderStatus(@ApiParam(value = "商家订单号", required = true) @RequestParam String stockordernumber){
        HttpClientResult httpClientResult = saiheyiService.orderStatus(stockordernumber);
        return httpClientResult;
    }

    @ApiOperation(value = "缴费回调通知(5)(暂时没有用)", notes = "缴费回调通知")
    @RequestMapping(value = "noticeurl", method = RequestMethod.POST)
    public Object noticeurl(HttpServletRequest request){
        System.out.println("进入赛合一缴费回调接口开始");
        JSONObject jsonObject = new JSONObject();
        try {
            /**分销商订单号*/
            String ordernumber = new String(request.getParameter("ordernumber").getBytes("ISO-8859-1"), "UTF-8");
            System.out.println("ordernumber:"+ordernumber);
            /**赛合一平台订单号*/
            String esupordernumber = new String(request.getParameter("esupordernumber").getBytes("ISO-8859-1"), "UTF-8");
            System.out.println("esupordernumber:"+esupordernumber);
            /**订单状态4-充值成功 5-充值失败*/
            String status = new String(request.getParameter("status").getBytes("ISO-8859-1"), "UTF-8");
            System.out.println("status:"+status);
            /**订单结束时间*/
            String finishedtime = new String(request.getParameter("finishedtime").getBytes("ISO-8859-1"), "UTF-8");
            System.out.println("finishedtime:"+finishedtime);
            /**签名（MD5）*/
            String sign = new String(request.getParameter("sign").getBytes("ISO-8859-1"), "UTF-8");
            System.out.println("sign:"+sign);
            /**时间戳*/
            String timestamp = new String(request.getParameter("timestamp").getBytes("ISO-8859-1"), "UTF-8");
            System.out.println("timestamp:"+timestamp);
        } catch (Exception e) {
            logger.error("赛合一缴费回调接口异常" + e);
            return null;
        }
        System.out.println("赛合一缴费回调接口结束");
        jsonObject.put("result","t");
        return jsonObject;
    }


//    @ApiOperation(value = "初始化户号缴费信息表", notes = "初始化户号缴费信息表")
//    @RequestMapping(value = "recharge/account/init", method = RequestMethod.POST)
//    public Object wecbillpost(@ApiParam(value = "户号", required = true) @RequestParam String wecAccount,
//                              @ApiParam(value = "市",required = true) @RequestParam String city,
//                              @ApiParam(value = "公司",required = true) @RequestParam String company,
//                              @ApiParam(value = "缴费类型 1：电 2：水 3：燃气",required = true) @RequestParam String type,
//                              @ApiParam(value = "付费类型 1：后付费 2：预付费",required = true) @RequestParam String rechargeType,
//                              @ApiParam(value = "运营管理系统中房产运营编号",required = false) @RequestParam String houseOperationNumber){
//        return rechargeAccountService.initRechargeAccount(wecAccount, city, company, type,rechargeType,houseOperationNumber);
//    }

    @ApiOperation(value = "水电煤缴费业务订单下单接口", notes = "水电煤缴费业务订单下单接口")
    @RequestMapping(value = "prepayment/wrecharge", method = RequestMethod.POST)
    public Object prepaymentRecharge(
                            @ApiParam(value = "滞纳金", required = true) @RequestParam String delayfee,
                            @ApiParam(value = "水电煤缴费账号", required = true) @RequestParam String wecaccount,
                            @ApiParam(value = "水电煤缴费金额", required = true) @RequestParam String wecbillmoney,
                            @ApiParam(value = "缴费类型 1电 2水 3燃气", required = true) @RequestParam Integer type){
        return saiheyiService.prepayment(wecaccount,delayfee , wecbillmoney,type);
    }




}
